export default {
    props: {
        //列表刷新
        getDataList: {
            type: Function,
            default: null
        },
    },
    data() {
        return {
            axios: this.$axiosAuth, // 请求的微服务，默认授权服务
            addUrl: '',//新增
            updateUrl: '',//修改
            byIdUrl: undefined,//获取一条
            dialog: {//弹框数据
                id: "addOrUpdateDialogId",
                fullscreen: false, // 弹框全屏
                open: false,//是否弹出表单
                title: '',//标题
                width: '500px', //定义宽度，默认400px
            },
            form: {//表单数据
                ref: 'addOrUpdateFormRef', //ref属性
                url: '',//表单提交接口
                rules: {},
                info: {},//表单提交数据
                updateDisabled: {}, //修改需要禁用的字段

            },
        }
    },
    methods: {
        //按钮点击事件
        initDialog(params) {
            let row = params.row
            let title = params.title
            let fun = params.fun
            //重置表单（包含校验和输入框）
            const xForm = this.$refs[this.form.ref];
            if (xForm) {
                xForm.reset();
            }
            //处理禁用的逻辑
            Object.keys(this.form.updateDisabled).forEach(key => {
                //修改禁用、新增不禁用
                this.form.updateDisabled[key] = !!(row && row.id);
            });
            if (row && row.id) {
                this.dialog.title = title || "修改"
                this.form.url = this.updateUrl;
                this.form.method = "put";
                if (this.byIdUrl) {
                    //防止重复点击按钮
                    this.setLoading(true)
                    //修改的时候先查询一下这一条数据进行回显
                    this.axios({
                        url: this.byIdUrl,
                        method: 'get',
                        params: {id: row.id}
                    }).then(res => {
                        this.form.info = res.data;
                        this.$nextTick(() => {
                            //外部传入一个方法来处理复杂情况
                            if (fun) {
                                fun(this.form.info);
                            }
                            //打开弹框
                            this.dialog.open = true;
                        })
                    }).finally(() => {
                        this.setLoading(false)
                    })
                } else {
                    //使用页面的数据
                    this.form.info = JSON.parse(JSON.stringify(row));
                    this.$nextTick(() => {
                        //外部传入一个方法来处理复杂情况
                        if (fun) {
                            fun(this.form.info);
                        }
                        //打开弹框
                        this.dialog.open = true;
                    })
                }
            } else {
                //有可能有查询条件
                if (row) {
                    this.form.info = JSON.parse(JSON.stringify(row));
                }
                this.dialog.title = title || "新增"
                this.form.url = this.addUrl;
                this.form.method = "post";
                this.$nextTick(() => {
                    //外部传入一个方法来处理复杂情况
                    if (fun) {
                        fun(this.form.info);
                    }
                    //打开弹框
                    this.dialog.open = true;
                })
            }
        },
        setLoading(val) {
            // 子组件设置父组件值
            this.$emit('update:loading', val)
        },
        //表单提交，如果data有值，代表在调用时处理了数据
        submitForm(data) {
            this.$refs[this.form.ref].validate((valid) => {
                if (!valid) {
                    //弹框的loading
                    let loading = this.$loading({target: `#${this.dialog.id} >div >.el-dialog`});
                    this.axios({
                        url: this.form.url,
                        method: this.form.method,
                        data: data || this.form.info
                    }).then(res => {
                        this.$modal.msgSuccess(res.msg);
                        this.dialog.open = false;
                        this.getDataList();
                    }).finally(() => {
                        loading.close()
                    });
                }
            })
        },
        //复制
        copyClick(params) {
            const xForm = this.$refs[params.ref];
            if (xForm) {
                xForm.reset();
            }
            this.dialog.title = params.title;
            this.form.info = JSON.parse(JSON.stringify(params.row));
            delete this.form.info.id
            this.form.info.sort++
            this.dialog.open = true;
            this.form.url = this.addUrl
            this.form.method = "post";
        },
    }
}
